import requests
import json
import re


class GoDaddyDNSUpdater:

    def __init__(self, api_key, api_secret, domain, record_name, proxy=None):
        self.api_key = api_key
        self.api_secret = api_secret
        self.domain = domain
        self.record_name = record_name
        self.proxy = proxy

    def update_dns_record(self, current_ip):
        update_url = f"https://api.godaddy.com/v1/domains/{self.domain}/records/A/{self.record_name}"
        update_data = [
            {
                "data": current_ip,
                "name": "@",
                "ttl": 600,
                "type": "A"
            }
        ]
        update_headers = {
            "Authorization": f"sso-key {self.api_key}:{self.api_secret}",
            "Content-Type": "application/json"
        }

        requests.put(update_url, headers=update_headers, data=json.dumps(update_data), proxies=self.proxy)

    def run(self, current_ip):
        if current_ip:
            print(f"提取到的 IP 地址为: {current_ip}")
            self.update_dns_record(current_ip)
            print(f"A记录已更新为 {current_ip}")
            return True
        else:
            print("未找到 IP 地址")
        return False
